package hiresoft.ui;

import hiresoft.network.*;
import hiresoft.users.Reference;
import javax.swing.*;

/**
 * Creates the User Interface and
 *  functionality for Reference accounts
 *
 * @author Kevin Carlino
 */
public class ReferenceFrame extends UserFrame {
    Network net;

    public ReferenceFrame(LoginFrame context, Reference _user) {
        super(context, _user);
        initComponents();
    }

    /**
     * Sets the network/connection and calls the main function.
     */
    public void connect() {
        try {
        net = Network.getInstance();
        getApp();
        }
        catch (Exception e) {
            JOptionPane.showMessageDialog(null, "No network connection established.");
        }
    }

    /**
     * Retrieves specific applicant information and displays it.
     */
    public void getApp() {
            try {
                net.out.Insert("");
                net.out.Send(Packets.P_REQUESTAPP);
                if (net.Ready())
                {
                    if (net.in.getPacketId() == Packets.P_REQUESTAPP)
                    {
                        if(net.in.getDWord() != 0)
                        {
                            lblApplicantName.setText(net.in.getString());
                            lblReferenceName.setText(net.in.getString());
                        }
                        else
                            JOptionPane.showMessageDialog(null, "Failed to retrieve application information.");
                    }
                }
            }
            catch (Exception e) {
                JOptionPane.showMessageDialog(null, "Error: " + e);
            }
    }

    /**
     * Sends reference information given by user.
     */
    public void submitReference() {
        try {
            String sendtext = txtResponse.getText();
            net.out.Insert(sendtext);
            net.out.Send(Packets.P_SENDREFERENCE);
            if (net.Ready())
            {
                if (net.in.getPacketId() == Packets.P_SENDREFERENCE)
                {
                        if(net.in.getDWord() != 0){
                            JOptionPane.showMessageDialog(null, "Response has been successfully sent.");
                            super.logout();
                        }else
                            JOptionPane.showMessageDialog(null, "Failed to send response.");
                }
            }
        }
        catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Error: " + e);
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        buttonGroup2 = new javax.swing.ButtonGroup();
        btnSubmit = new javax.swing.JButton();
        lblResponse = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtResponse = new javax.swing.JTextArea();
        lblAppName = new javax.swing.JLabel();
        lblRefName = new javax.swing.JLabel();
        lblApplicantName = new javax.swing.JLabel();
        lblReferenceName = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        btnSubmit.setText("Submit");
        btnSubmit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSubmitActionPerformed(evt);
            }
        });

        lblResponse.setText("Response:");

        txtResponse.setColumns(20);
        txtResponse.setRows(5);
        txtResponse.setText("Enter Your Response");
        jScrollPane1.setViewportView(txtResponse);

        lblAppName.setText("Applicant Name:");

        lblRefName.setText("Reference Name:");

        lblApplicantName.setText("Joe Smith");

        lblReferenceName.setText("Jane Smith");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 449, Short.MAX_VALUE)
                    .addComponent(btnSubmit, javax.swing.GroupLayout.DEFAULT_SIZE, 449, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addComponent(lblAppName)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(lblApplicantName))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addComponent(lblRefName)
                            .addGap(18, 18, 18)
                            .addComponent(lblReferenceName)))
                    .addComponent(lblResponse, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblAppName)
                    .addComponent(lblApplicantName))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblRefName)
                    .addComponent(lblReferenceName))
                .addGap(18, 18, 18)
                .addComponent(lblResponse)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 323, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnSubmit)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnSubmitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSubmitActionPerformed
        submitReference();
    }//GEN-LAST:event_btnSubmitActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnSubmit;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.ButtonGroup buttonGroup2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblAppName;
    private javax.swing.JLabel lblApplicantName;
    private javax.swing.JLabel lblRefName;
    private javax.swing.JLabel lblReferenceName;
    private javax.swing.JLabel lblResponse;
    private javax.swing.JTextArea txtResponse;
    // End of variables declaration//GEN-END:variables

}
